package ru.spbu.math.android.cbrf.view;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import ru.spbu.math.android.cbrf.R;
import ru.spbu.math.android.cbrf.R.id;
import ru.spbu.math.android.cbrf.R.layout;
import ru.spbu.math.android.cbrf.dataprovider.CbrfData;
import ru.spbu.math.android.cbrf.model.Rate;

import java.util.*;

public class CourseList extends Activity {

    private final static String NAME = "Name";
    private final static String CHAR_CODE = "CharCode";
    private final static String VALUE = "Value";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.course_list);
        ListView listView = (ListView) findViewById(R.id.ListView01);

        CbrfData cbrfData = new CbrfData();
        Date startDate = new Date(110, 0, 0);
        Date endDate = new Date(110, 3, 9);
        List<Rate> currList = new ArrayList<Rate>();
//        List<Rate> currList = cbrfData.getDynamicRateList(startDate, endDate, "USD");     //todo: uncomment in future; commented by the Yan Lobkarev because cannot build the project;
        //List<Rate> currList = cbrfData.getDynamicCountRateList(startDate, endDate, "USD", 3);

        LinkedList<Map<String, String>> list = convertListToRaw(currList);
        SimpleAdapter adapter = new SimpleAdapter(this, list,
                R.layout.course_list_row, new String[]{NAME,
                        CHAR_CODE, VALUE}, new int[]{R.id.name,
                        R.id.CHAR_CODE, R.id.VALUE});
        listView.setAdapter(adapter);
    }

    //��������� ������ ����� � ������ ��� ListView
    private static LinkedList<Map<String, String>> convertListToRaw(
            List<Rate> currList) {

        LinkedList<Map<String, String>> list = new LinkedList<Map<String, String>>();
        HashMap<String, String> raw;

        for (Rate currency : currList) {
            raw = new HashMap<String, String>();
            raw.put(NAME, currency.getSource().getName());
            raw.put(CHAR_CODE, currency.getSource().getCode());
            raw.put(VALUE, Double.toString(currency.getValue()));
            list.add(raw);
        }

        return list;
    }
}
